
let url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx2be380fdde5b4d4f&secret=aab61b48e5b9b7d45485ac93feaa9107'
Page({
  data: {
    openid: null,
  },

  submit() {

    wx.requestSubscribeMessage({
      tmplIds: ['KpUkKgyuZX6bK1I63cStvXNoNt2_UaqbIvpUiYeM8zA'],
      success: res => {
        console.log(res)
        this.getOpenId(res => {
          this.data.openid = res.data.openid;
          this.getAccessToken(res => {
            console.log(res.data.access_token)
            var data = {
              "touser": this.data.openid,
              "template_id": "KpUkKgyuZX6bK1I63cStvXNoNt2_UaqbIvpUiYeM8zA",
              "page": "index",
              "data": {
                // 订单编号
                "character_string1": {
                  "value": "123213"
                },
                // 订单状态
                "phrase2": {
                  "value": "出货中"
                },
                // 订单详细
                "thing3": {
                  "value": "哈哈哈哈"
                },
                // 产品名称
                "thing6": {
                  "value": "柳州职业技术学院官塘校区"
                },
                // 备注
                "thing7": {
                  "value": "柳州职业技术学院官塘校区"
                }
              },
            }
            wx.request({
              method: 'POST',
              url: 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' + res.data.access_token,
              data: data,
              success: res => {
                console.log(res)
                if (res.data.errcode == 0) {
                  wx.showToast({
                    title: '提交成功',
                    duration: 2000
                  })
                }
              }
            })
          })
        })
      }
    })
  },
  // 获取openid
  getOpenId(success) {
    wx.login({
      success(res) {
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session',
          data: {
            appid: 'wx2be380fdde5b4d4f',
            secret: 'aab61b48e5b9b7d45485ac93feaa9107',
            grant_type: 'authorization_code',
            js_code: res.code
          },
          success: success
        })
      }
    })
  },

  //获取 access_token,
  getAccessToken(success) {
    wx.request({
      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx2be380fdde5b4d4f&secret=aab61b48e5b9b7d45485ac93feaa9107',
      success: success
    })
  },
  onLoad(){
    this.chooseAddress()
  },
  chooseAddress(){
    var that = this;
    if (wx.chooseAddress) {
      wx.chooseAddress({
        success: function (res) {
          console.log(JSON.stringify(res));
          console.log(res);
          that.setData({
            "add_userName": res.userName,
            "add_telNumber": res.telNumber,
            "add_provinceName": res.provinceName,
            "add_cityName": res.cityName,
            "add_countyName": res.countyName,
            "add_detailInfo": res.detailInfo,
            "add_postalCode": res.postalCode,
            //具体收货地址显示
            flag:false,

          })
        },
        fail: function (err) {
          console.log(JSON.stringify(err));
          console.info("收货地址授权失败");
          wx.showToast({
            title: '授权失败，您将无法进行下单支付;重新授权请删除小程序后再次进入',
            icon: 'success',
            duration: 20000
          })
        }
      })
    } else {
      console.log('当前微信版本不支持chooseAddress');
    }
  }


})
